package com.zzw.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.common.Constants;
import com.zzw.springboot.common.Result;
import com.zzw.springboot.config.AuthAccess;
import com.zzw.springboot.entity.Files;
import com.zzw.springboot.entity.User;
import com.zzw.springboot.service.IFilesService;
import com.zzw.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author zzw2000
 * @Date 2022年05月28日 17:18
 * @Description Echarts图表控制器
 */
@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private IUserService userService;
    @Resource
    private IFilesService filesService;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/example")
    public Result get() {
        Map<String, Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
        return Result.success(map);
    }

    @GetMapping("/members")
    public Result members() {
        List<User> users = userService.list();
        int q1 = 0, q2 = 0, q3 = 0, q4 = 0; //四个季度ji'du
        for (User user : users) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1:
                    q1 += 1;
                    break;
                case Q2:
                    q2 += 1;
                    break;
                case Q3:
                    q3 += 1;
                    break;
                case Q4:
                    q4 += 1;
                    break;
                default:
                    break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1, q2, q3, q4));
    }

    @AuthAccess
    @GetMapping("/file/front/all")
    public Result frontAll(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "10") int pageSize) {
        // 1. 从缓存获取数据
        String jsonStr = (String) redisTemplate.opsForValue().get(Constants.FILES_KEY);
        //List<Files> files;
        Page<Files> pageFiles;
        // 2. 取出来的json是空的
        if (StringUtils.isBlank(jsonStr)) {
            // 3. 从数据库取出数据
            //files = filesService.list();
            pageFiles = filesService.page(new Page<>(pageNum, pageSize));
            // 4. 再去缓存到redis
            redisTemplate.opsForValue().set(Constants.FILES_KEY,JSONUtil.toJsonStr(pageFiles),2, TimeUnit.HOURS);
        } else {
            // 5. 如果有，从redis缓存中获取数据
            pageFiles = JSONUtil.toBean(jsonStr, new TypeReference<>() {
            }, true);
        }
        return Result.success(pageFiles);
    }
}
